#include "../inc/URL.h"
#include <assert.h>


//	URL::URL(){
//		url=NULL;
//		base=NULL;
//		path=NULL;
//	}

	ostream &operator<<(ostream &stream, URL o){
	  stream << o.getAddress();;
	  return stream;
	}

	URL::URL(string *address){
		//TODO: Check for no white space
		assert(address!=NULL);
		url= new string(*address);
		//Construct base address and path
		int i=0;
		int slashes=0;
		while (slashes<3 && i<(int)(address->length())){
			if ((*address)[i]=='/')
				slashes++;
			i++;
		}
		base = new string(address->substr(0,i));
		if (i ==(int)(address->length()))
			path= new string("");
		else
			path= new string(address->substr(i,address->length()-i));
	}

	URL::URL(const URL &other){
		assert(&other!=NULL);
			url=new string(*(other.url));
			base=new string(*(other.base));
			path=new string(*(other.path));
	}

	URL::~URL(){
			delete url;
			delete path;
			delete base;
	}

	URL& URL::operator =(const URL &other){
		assert(&other!=NULL);
		if (this!=&other){
			delete url;
			delete base;
			delete path;
			url=new string(*(other.url));
			base=new string(*(other.base));
			path=new string(*(other.path));
		}
		return *this;
	}

	string URL::getAddress(){
		return *url;
	}

	string URL::getBaseAddr(){
		return *base;
	}

	string URL::getPath(){
		return *path;
	}

	int URL::compare(const URL &other){
		assert(&other!=NULL);
		if (strcasecmp(base->c_str(),other.base->c_str())==0 && path->compare(*(other.path))==0)
			return 0;
		return url->compare(*(other.url));
	}

	bool URL::inScope(URL &other){
		assert(&other!=NULL);
		if(strcasecmp(base->c_str(),other.base->c_str())==0 && other.getPath().find(*path)!=string::npos)
			return true;
		return false;

	}

	string URL::print(){
		return string(*url + ":" + *base + "," + *path);
	}

